package info.niwota.vi;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

/**
 * 
 * @author qiangli
 * 
 */
public final class AppUtils {
	private AppUtils() {
	}
	
	/**
	 * 1. replace lf/cr with space
	 * 2. trim leading/trailing spaces
	 * @param s
	 * @return
	 */
	public static String normalize(String s) {
		if (s == null) {
			return "";
		}
		final String regex = "[\\s]+";
		return s.replaceAll(regex, " ").trim();
	}

	public static void startSendActivity(Context ctx, String text) {
		try {
			Intent i = new Intent();
			i.setAction(Intent.ACTION_SEND);
			i.setType("message/rfc822");
			// i.putExtra(Intent.EXTRA_SUBJECT, "");
			i.putExtra(Intent.EXTRA_TEXT, text);
			ctx.startActivity(Intent.createChooser(i, ""));
		} catch (ActivityNotFoundException e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		}
	}

	public static void startMmsActivity(Context ctx, String text) {
		try {
			Intent i = new Intent();
			i.setAction(Intent.ACTION_VIEW);
			// i.putExtra("address", "");
			i.putExtra("sms_body", text);
			i.setType("vnd.android-dir/mms-sms");
			ctx.startActivity(Intent.createChooser(i, ""));
		} catch (ActivityNotFoundException e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		}
	}

	public static void startBrowseActivity(Context ctx, String uri) {
		try {
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.addCategory(Intent.CATEGORY_BROWSABLE);
			i.setData(Uri.parse(uri));
			ctx.startActivity(i);
		} catch (ActivityNotFoundException e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		}
	}
}
